package com.yupi.springbootinit.controller;

import cn.hutool.core.bean.BeanUtil;
import com.yupi.springbootinit.common.BaseResponse;
import com.yupi.springbootinit.common.ResultUtils;
import com.yupi.springbootinit.mapper.ClickCountMapper;
import com.yupi.springbootinit.model.entity.ClickCount;
import com.yupi.springbootinit.model.vo.ClickCountVo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description 点击计数
 * @Author minify
 * @Date 2023/11/13 10:28
 * @Project count-backend
 */
@RequestMapping("/click")
@RestController
public class ClickController {

    @Resource
    private ClickCountMapper clickCountMapper;

    @GetMapping("/add")
    public BaseResponse<ClickCountVo> add() {
        List<ClickCount> clickCounts =
                clickCountMapper.selectList(null);

        ClickCount clickCount = clickCounts.get(0);
        clickCount.setCount(clickCount.getCount() + 1);
        clickCountMapper.updateById(clickCount);

        ClickCountVo clickCountVo = BeanUtil.copyProperties(clickCount, ClickCountVo.class);

        return ResultUtils.success(clickCountVo);
    }

    @GetMapping("sub")
    public BaseResponse<ClickCountVo> sub() {
        List<ClickCount> clickCounts =
                clickCountMapper.selectList(null);

        ClickCount clickCount = clickCounts.get(0);
        clickCount.setCount(clickCount.getCount() - 1);
        clickCountMapper.updateById(clickCount);

        ClickCountVo clickCountVo = BeanUtil.copyProperties(clickCount, ClickCountVo.class);

        return ResultUtils.success(clickCountVo);
    }

    /**
     * 重置
     */
    @GetMapping("/reset")
    public BaseResponse<ClickCountVo> reset() {
        List<ClickCount> clickCounts =
                clickCountMapper.selectList(null);

        ClickCount clickCount = clickCounts.get(0);
        clickCount.setCount(0);
        clickCountMapper.updateById(clickCount);

        ClickCountVo clickCountVo = BeanUtil.copyProperties(clickCount, ClickCountVo.class);

        return ResultUtils.success(clickCountVo);
    }

    /**
     * 返回总数
     */
    @GetMapping("/total")
    public BaseResponse<ClickCountVo> total() {
        List<ClickCount> clickCounts =
                clickCountMapper.selectList(null);

        ClickCount clickCount = clickCounts.get(0);

        ClickCountVo clickCountVo = BeanUtil.copyProperties(clickCount, ClickCountVo.class);

        return ResultUtils.success(clickCountVo);
    }
}
